﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vITs.Site.Models.DataModels;

namespace vITs.Site.Controllers
{
    public class ConsultantController : Controller
    {
        [Authorize(Roles="Konsult")]
        [HttpGet]
        public ActionResult AssignmentList()
        {
            VitsDB db = new VitsDB();
            User user = db.Users.SingleOrDefault(u => u.EmailAddress == this.User.Identity.Name);
            if (user == null) return this.RedirectToAction("Index", "Dashboard");

            this.ViewBag.User = user;

            #region Fulhack, fixa senare

            var groupedAssignments =
                from Assignment a in user.Assignments
                group a by ((int)a.Status) into grp
                where grp.Count() > 0
                orderby grp.Key ascending
                select new { Key = grp.Key, Items = grp };

            Dictionary<int, Assignment[]> foobar = new Dictionary<int, Assignment[]>();
            foreach (var item in groupedAssignments)
            {
                List<Assignment> _a = new List<Assignment>();

                foreach (var obj in item.Items)
                {
                    _a.Add(obj);
                }

                foobar.Add(item.Key, _a.ToArray());
            }

            #endregion

            this.ViewBag.Assignments = foobar;

            return View();
        }

        [HttpGet]
        [Authorize(Roles = "Konsult")]
        public ActionResult ViewAssignment(int AssignmentId)
        {
            var assignment = (from Assignment a in new VitsDB().Assignments where a.AssignmentId == AssignmentId select a).SingleOrDefault();
            if (assignment == null) return this.RedirectToAction("Index", "Dashboar");

            this.ViewBag.Assignment = assignment;
            this.ViewBag.IsEditable = assignment.IsEditable();

            return this.View();
        }
    }
}
